/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Category.java
 *
 * Created on Oct 29, 2013, 10:15:15 PM
 */
package sims.gui;

import java.util.List;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import sims.dao.CategoryDao;
import sims.entity.TbCategory;
import sims.library.Validate;

/**
 *
 * @author NINH
 */
public class Category extends javax.swing.JPanel {

    CategoryDao cateDao;
    Validate valid;

    /** Creates new form Category */
    public Category() {
        initComponents();
        cateDao = new CategoryDao();
        valid = new Validate();
        loadData();
        disableTextbox();
    }

    public void loadData() {
        tblCategory.setModel(DisplayAll());
    }

    public DefaultTableModel DisplayAll() {
        Vector vtable = new Vector();
        Vector vrow = new Vector();
        Vector vheader = new Vector();
        List<TbCategory> categories = cateDao.getAll(TbCategory.class);

        vheader.add("Id");
        vheader.add("Name");
        vheader.add("Code");
        vheader.add("Description");

        for (TbCategory cate : categories) {
            vrow.add(cate.getCateId());
            vrow.add(cate.getCateName());
            vrow.add(cate.getCateCode());
            vrow.add(cate.getCateDes());
            vtable.add(vrow);
            vrow = new Vector();
        }
        return new DefaultTableModel(vtable, vheader);
    }

    public void Reset() {
        txtCategoryId.setText("");
        txtCategoryName.setText("");
        txtCode.setText("");
        txtDescription.setText("");
    }

    public void binding() {
        int row = tblCategory.getSelectedRow();
        String id = tblCategory.getValueAt(row, 0).toString();
        String name = tblCategory.getValueAt(row, 1).toString();
        String code = tblCategory.getValueAt(row, 2).toString();
        String description = tblCategory.getValueAt(row, 3).toString();

        txtCategoryId.setText(id);
        txtCode.setText(code);
        txtCategoryName.setText(name);
        txtDescription.setText(description);
    }
    public void disableTextbox(){
        txtCategoryName.setEnabled(false);
        txtCode.setEnabled(false);
        txtDescription.setEnabled(false);
    }
    public void enableTextbox(){
        txtCategoryName.setEnabled(true);
        txtCode.setEnabled(true);
        txtDescription.setEnabled(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtCategoryName = new javax.swing.JTextField();
        txtCode = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtDescription = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        btnUpdate = new javax.swing.JButton();
        btnAdd = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        lblDescription = new javax.swing.JLabel();
        lblCategoryName = new javax.swing.JLabel();
        lblCode = new javax.swing.JLabel();
        txtCategoryId = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        lblCateId = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblCategory = new javax.swing.JTable();

        setBackground(new java.awt.Color(255, 255, 255));
        setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Category manager", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N
        setPreferredSize(new java.awt.Dimension(1046, 530));
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("Category name: ");
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 30, 100, 20));

        txtCategoryName.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtCategoryNameFocusLost(evt);
            }
        });
        jPanel1.add(txtCategoryName, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 30, 270, -1));
        jPanel1.add(txtCode, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 60, 270, -1));

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setText("Category code: ");
        jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 60, 100, 20));
        jPanel1.add(txtDescription, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 90, 270, -1));

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel3.setText("Category description: ");
        jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 90, -1, 20));

        btnUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sims/images/edit.png"))); // NOI18N
        btnUpdate.setText("Edit");
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });
        jPanel1.add(btnUpdate, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 120, -1, 24));

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sims/images/add.png"))); // NOI18N
        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });
        jPanel1.add(btnAdd, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 120, 90, 24));

        btnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sims/images/delete.png"))); // NOI18N
        btnDelete.setText("Delete");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });
        jPanel1.add(btnDelete, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 120, -1, 24));

        lblDescription.setFont(new java.awt.Font("Tahoma", 2, 11));
        lblDescription.setForeground(new java.awt.Color(255, 0, 0));
        jPanel1.add(lblDescription, new org.netbeans.lib.awtextra.AbsoluteConstraints(460, 90, 510, 20));

        lblCategoryName.setFont(new java.awt.Font("Tahoma", 2, 11));
        lblCategoryName.setForeground(new java.awt.Color(255, 0, 0));
        jPanel1.add(lblCategoryName, new org.netbeans.lib.awtextra.AbsoluteConstraints(460, 30, 510, 20));

        lblCode.setFont(new java.awt.Font("Tahoma", 2, 11));
        lblCode.setForeground(new java.awt.Color(255, 0, 0));
        jPanel1.add(lblCode, new org.netbeans.lib.awtextra.AbsoluteConstraints(460, 60, 510, 20));

        txtCategoryId.setEnabled(false);
        jPanel1.add(txtCategoryId, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 0, 270, -1));

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel7.setText("Category Id: ");
        jPanel1.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 100, 20));

        lblCateId.setFont(new java.awt.Font("Tahoma", 2, 11));
        lblCateId.setForeground(new java.awt.Color(255, 0, 0));
        jPanel1.add(lblCateId, new org.netbeans.lib.awtextra.AbsoluteConstraints(460, 0, 510, 20));

        add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 280, 1030, 260));

        jScrollPane1.setBackground(new java.awt.Color(255, 255, 255));

        tblCategory.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblCategory);

        add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, 1030, 250));
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
        if (btnAdd.getText().equalsIgnoreCase("add")) {
            enableTextbox();
            Reset();
            btnAdd.setText("Save");
            tblCategory.setEnabled(false);
            btnUpdate.setEnabled(false);
            btnDelete.setText("Cancel");
        } else {
            try {
                String cateCode = txtCode.getText();
                String cateName = txtCategoryName.getText();
                String cateDescription = txtDescription.getText();

                TbCategory cate = new TbCategory();
                cate.setCateCode(cateCode);
                cate.setCateName(cateName);
                cate.setCateDes(cateDescription);

                cateDao.insert(cate);
                JOptionPane.showMessageDialog(null, "Insert success.");
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "Insert error.");
            }
            loadData();
            disableTextbox();
            Reset();
            btnAdd.setText("Add");
            tblCategory.setEnabled(true);
            btnUpdate.setEnabled(true);
            btnDelete.setText("Delete");
        }
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        // TODO add your handling code here:
        if (btnDelete.getText().equalsIgnoreCase("delete")) {
            if (tblCategory.getSelectedRow() == -1) {
                JOptionPane.showMessageDialog(null, "Select category to delete.");
                return;
            } else {
                int check = JOptionPane.showConfirmDialog(null, "Are you sure");
                if (check == JOptionPane.OK_OPTION) {
                    int rowIndex = tblCategory.getSelectedRow();
                    int cateId = Integer.parseInt(tblCategory.getValueAt(rowIndex, 0).toString());
                    TbCategory cate = (TbCategory) cateDao.getById(TbCategory.class, cateId);
                    cateDao.delete(cate);
                    JOptionPane.showMessageDialog(null, "Delete success.");
                    loadData();
                }
            }
        } else {
            disableTextbox();
            tblCategory.setEnabled(true);
            btnAdd.setText("Add");
            btnAdd.setEnabled(true);
            btnUpdate.setText("Edit");
            btnUpdate.setEnabled(true);
            btnDelete.setText("Delete");
            Reset();
        }
    }//GEN-LAST:event_btnDeleteActionPerformed

    private void txtCategoryNameFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtCategoryNameFocusLost
        // TODO add your handling code here:
    }//GEN-LAST:event_txtCategoryNameFocusLost

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
        // TODO add your handling code here:
        if (btnUpdate.getText().equalsIgnoreCase("edit")) {
            if (tblCategory.getSelectedRow() == -1) {
                JOptionPane.showMessageDialog(null, "Select category to edit.");
                return;
            } else {
                enableTextbox();
                binding();
                btnUpdate.setText("Save");
                tblCategory.setEnabled(false);
                btnAdd.setEnabled(false);
                btnDelete.setText("Cancel");
            }
        } else {
            int id = Integer.parseInt(txtCategoryId.getText());
            String name = txtCategoryName.getText();
            String code = txtCode.getText();
            String description = txtDescription.getText();

            TbCategory cate = new TbCategory();
            cate.setCateId(id);
            cate.setCateName(name);
            cate.setCateCode(code);
            cate.setCateDes(description);

            cateDao.update(cate);
            JOptionPane.showMessageDialog(null, "Update success.");
            loadData();
            disableTextbox();
            Reset();

            btnUpdate.setText("Edit");
            tblCategory.setEnabled(true);
            btnAdd.setEnabled(true);
            btnDelete.setText("Delete");
        }
    }//GEN-LAST:event_btnUpdateActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblCateId;
    private javax.swing.JLabel lblCategoryName;
    private javax.swing.JLabel lblCode;
    private javax.swing.JLabel lblDescription;
    private javax.swing.JTable tblCategory;
    private javax.swing.JTextField txtCategoryId;
    private javax.swing.JTextField txtCategoryName;
    private javax.swing.JTextField txtCode;
    private javax.swing.JTextField txtDescription;
    // End of variables declaration//GEN-END:variables
}
